﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Basic_maildustbin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tid"] == null)
            Response.Redirect("../Error/Error.htm");
        if (!IsPostBack)
        {
            BindGridView(true);
        }
    }
    protected void CheckAll(object sender, EventArgs e)
    {
        CheckBox cbx = (CheckBox)sender;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("ItemBox");
            ch.Checked = cbx.Checked;
        }
    }
    protected void ddlFrom_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView(true);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView(false);
    }
    /// <summary>
    /// 处理收发件人姓名
    /// </summary>
    /// <param name="insta">收件箱的状态</param>
    /// <param name="inname">发件人姓名</param>
    /// <param name="outname">收件人姓名</param>
    /// <returns>返回标题内容</returns>
    static public string strName(string insta, string inname,string outname)
    {
        string tmpStr = "";
        if (insta == "1")
            tmpStr = inname;
        else
            tmpStr = outname;
        return tmpStr;
    }
    /// <summary>
    /// 绑定gridview
    /// </summary>
    /// <param name="GetCount">是否获取总数</param>
    protected void BindGridView(bool GetCount)
    {
        if (Session["tid"] == null)
            return;
        string tid = Session["tid"].ToString().Trim();
        string field = "id,name1,name2,title,instatus,outstatus";
        StringBuilder where = new StringBuilder();
        if (this.ddlFrom.SelectedValue == "2")
            where.Append(" sender='").Append(tid).Append("' and outstatus=1");
        else if (this.ddlFrom.SelectedValue == "1")
            where.Append(" receiver='").Append(tid).Append("' and instatus=1");
        else
        {
            where.Append(" (sender='").Append(tid).Append("' or receiver='").Append(tid).Append("')");
            where.Append(" and (instatus=1 or outstatus=1)");
        }

        PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "View_Messages", "id", field, "id desc", where.ToString(), GetCount);
    }
    //批量删除
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.ddlFrom.SelectedValue == "1")
            DeleteByBunch("instatus=2");
        else
            DeleteByBunch("outstatus=2");
    }
    //批量恢复
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (this.ddlFrom.SelectedValue == "1")
            DeleteByBunch("instatus=0");
        else
            DeleteByBunch("outstatus=0");
    }
    protected void DeleteByBunch(string target)
    {
        Hashtable sqlstrs = new Hashtable();
        int keyid = -1;
        int i = 1;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox check = (CheckBox)gvr.FindControl("ItemBox");
            if (check.Checked)
            {
                keyid = Int32.Parse(GridView1.DataKeys[gvr.DataItemIndex].Value.ToString().Trim());
                string param = "@mailid" + i.ToString();
                string sqlstr = "update Basic_UserMail set "+target+" where id=" + param;
                SqlParameter[] MailParms = new SqlParameter[1];
                MailParms[0] = new SqlParameter(param, keyid);
                sqlstrs.Add(sqlstr, MailParms);
                i++;
            }
        }
        if (i > 1)
        {
            PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstrs);
            BindGridView(true);
        }
    }
    //行内命令（恢复单个）
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string mid = e.CommandArgument.ToString().Trim();
        if (e.CommandName == "getback")
        {
            PM.PMBasic.Mail Mail = new PM.PMBasic.Mail();
            if(this.ddlFrom.SelectedValue=="1")
                Mail.GetMsgBack(mid, "instatus=0");
            else
                Mail.GetMsgBack(mid, "outstatus=0");
        }
        BindGridView(true);
    }
}
